// Byte formatting utilities

pub fn format_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB", "PB"];
    
    if bytes == 0 {
        return "0 B".to_string();
    }

    let base = 1024_f64;
    let exponent = (bytes as f64).log(base).floor() as usize;
    let exponent = exponent.min(UNITS.len() - 1);
    
    let value = bytes as f64 / base.powi(exponent as i32);
    
    format!("{:.2} {}", value, UNITS[exponent])
}

pub fn format_bandwidth(bps: u64) -> String {
    const UNITS: &[&str] = &["bps", "Kbps", "Mbps", "Gbps", "Tbps"];
    
    if bps == 0 {
        return "0 bps".to_string();
    }

    let base = 1000_f64;
    let exponent = (bps as f64).log(base).floor() as usize;
    let exponent = exponent.min(UNITS.len() - 1);
    
    let value = bps as f64 / base.powi(exponent as i32);
    
    format!("{:.2} {}", value, UNITS[exponent])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(0), "0 B");
        assert_eq!(format_bytes(1024), "1.00 KB");
        assert_eq!(format_bytes(1048576), "1.00 MB");
        assert_eq!(format_bytes(1073741824), "1.00 GB");
    }

    #[test]
    fn test_format_bandwidth() {
        assert_eq!(format_bandwidth(0), "0 bps");
        assert_eq!(format_bandwidth(1000), "1.00 Kbps");
        assert_eq!(format_bandwidth(1000000), "1.00 Mbps");
        assert_eq!(format_bandwidth(1000000000), "1.00 Gbps");
    }
}
